<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/page/common/commonConfig.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>注册信息</title>
	<script type="text/javascript" src="<%=basePath %>component/notify.js"></script>
	<script type="text/javascript" src="<%=basePath%>dwrsystem/engine.js"></script>
	<script type="text/javascript" src="<%=basePath%>dwrsystem/interface/licenseDataService.js"></script>
	
	<script type="text/javascript">
	var NO_LICENSE = "<%=com.sinosoft.perfect.system.license.bean.LicenseContainerBean.NO_LICENSE%>";
	var READ_ERROR_LICENSE = "<%=com.sinosoft.perfect.system.license.bean.LicenseContainerBean.READ_ERROR_LICENSE%>";
	var RIGHT_LICENSE = "<%=com.sinosoft.perfect.system.license.bean.LicenseContainerBean.RIGHT_LICENSE%>";
	var ERROR_LICENSE = "<%=com.sinosoft.perfect.system.license.bean.LicenseContainerBean.ERROR_LICENSE%>";
	var OUT_LICENSE = "<%=com.sinosoft.perfect.system.license.bean.LicenseContainerBean.OUT_LICENSE%>";
	var tempSerial;
	var userId = '${user.userMainId}';
	
	function register(){
		licenseDataService.registerLicense({licenseName:$('#licenseName').val(),licenseSerial:$('#licenseSerial').val()},
			function(data){
				if(data == 'ok'){
					tempSerial = $('#licenseSerial').val();
					successfulRegisterInfo();
				}
				if(data == 'licenseError'){
					notifyWarn('注册失败,请购买正确的注册信息！');
					return;
				}
		});
	}
	
	function successfulRegisterInfo(){
		licenseDataService.initLicenseForShow(function(license){
			$('#licenseMachinceNumber').html(license.licenseMechine);
			$('#licenseMachinceName').html(license.licenseName);
			$('#licenseSerialNumber').html(license.licenseSerial);
			$('#startTime').html(getFormatTime(new Date(license.licenseStart-0)));
			$('#endTime').html(getFormatTime(new Date(license.licenseEnd-0)));
			
			$('#reloadRegisterButton').css('display','');
			$('#deleteLicenseButton').css('display','');
			
			$('#licenseInfo').css('display','none');
			$('#editLicenseInfo').css('display','');
		});
		
		if(userId == null || userId == '' || userId == 111){
			notifyCreater("成功注册!");
			$('#goToSystem').css('display','');
		}else{
			$('#goToSystem').css('display','none');
			parent.notifyCreater("成功注册!");
		}
	}
	
	function getFormatTime(date){
		return date.getFullYear() + "年" + (date.getMonth()+1) + "月" + date.getDate() +"日 " + date.getHours() + "时" + date.getMinutes() + "分" + date.getSeconds() + "秒";
	}
	
	function validateLicenseStatus(){
		licenseDataService.getLicenseStatus(function(data){
			licenseArea(data);
		});
	}
	
	function licenseArea(licenseStatus){
		if(licenseStatus == READ_ERROR_LICENSE){
			$('#READ_ERROR_LICENSE').css('display',''); 
		}else if(licenseStatus == RIGHT_LICENSE || licenseStatus == OUT_LICENSE){
			licenseDataService.initLicenseForShow(function(license){
				tempSerial = license.licenseSerial;
				$('#licenseMachinceNumber').html(license.licenseMechine);
				$('#licenseMachinceName').html(license.licenseName);
				$('#licenseSerialNumber').html(license.licenseSerial);
				$('#startTime').html(getFormatTime(new Date(license.licenseStart-0)));
				$('#endTime').html(getFormatTime(new Date(license.licenseEnd-0)));
				
				$('#reloadRegisterButton').css('display','');
				$('#deleteLicenseButton').css('display','');
				
				if(userId == null || userId == ''){
					$('#goToSystem').css('display','');
				}else{
					$('#goToSystem').css('display','none');
				}
				
				$('#licenseInfo').css('display','none');
				$('#editLicenseInfo').css('display','');
			});
		}else {
			initLicenseData();
			$('#licenseInfo').css('display',''); 
		}
		
		if(licenseStatus == NO_LICENSE){
			$('#registerButton').css('display','');
		}else{
			$('#reloadRegisterButton').css('display','');
			$('#deleteLicenseButton').css('display','');
			if(userId == null || userId == ''){
				$('#goToSystem').css('display','none');
			}else{
				$('#goToSystem').css('display','');
			}
		}
	}

	function initLicenseData(){
		licenseDataService.initLicense(function(license){
			$('#licenseMechine').html(license.licenseMechine);
			$('#licenseName').val(license.licenseName);
			$('#licenseSerial').val(license.licenseSerial);
		});
	}
	
	$(function (){
		if('${param.userId}' == 111){
			userId = 111;
		}
		validateLicenseStatus();
	});
	
	function reloadRegister(){
		$('#licenseMechine').html($('#licenseMachinceNumber').html());
		$('#licenseName').val($('#licenseMachinceName').html());
		$('#licenseSerial').val(tempSerial);
		
		$('#registerButton').css('display','');
		
		$('#licenseInfo').css('display','');
		$('#editLicenseInfo').css('display','none');
	}
	
	function deleteRegister(){
		licenseDataService.deleteLicense({licenseName:$('#licenseMachinceName').html(),licenseSerial:tempSerial},function(data){
			if(data == 'ok'){
				if('${user.userMainId}' == null || '${user.userMainId}' == ''){
					parent.parent.parent.document.location.href = "<%=basePath %>";
				}else{
					parent.parent.parent.document.location.href = "<%=basePath %>?userId="+111;
				}
			}
		});
	}
	</script>
</head>
<body>
	<div id="licenseInfo" style="display:none;" class="formlayout">
		<table>
			<tr>
				<th>应用号码</th>
				<td>
					<span id="licenseMechine"></span>
				</td>
			</tr>
			<tr>
				<th>注册名称</th>
				<td>
					<input name="licenseName" id="licenseName"  />
				</td>
			</tr>
			<tr>
				<th>注册序列号</th>
				<td>
					<textarea name="licenseSerial" id="licenseSerial"  ></textarea>
				</td>
			</tr>
			<tr>
				<td colspan="2">
					<button id="registerButton" onclick="javascript:register();" style="display: none;">注册</button>
				</td>
			</tr>
		</table>
	</div>
	<div id="READ_ERROR_LICENSE" style="display:none;color:red;" class="formlayout">
		注册信息读取错误，请重新启动服务器，检查license信息；如果仍然有问题，请联系系统相关工程师或者管理员
	</div>
	<div id="editLicenseInfo" style="display: none;" class="formlayout">
		<table>
			<tr>
				<th style="width: 150px;">应用号码</th><td><span id="licenseMachinceNumber"></span></td>
			</tr>
			<tr>
				<th style="width: 150px;">注册名称</th><td><span id="licenseMachinceName"></span></td>
			</tr>
			<tr>
				<th style="width: 150px;">注册序列号</th><td><span id="licenseSerialNumber"></span></td>
			</tr>
			<tr>
				<th style="width: 150px;">开始时间</th><td><span id="startTime"></span></td>
			</tr>
			<tr>
				<th style="width: 150px;">结束时间</th><td><span id="endTime"></span></td>
			</tr>
			<tr>
				<td colspan="2">
					<button id="reloadRegisterButton" style="display: none;" onclick="reloadRegister();">重新注册</button>
					<button id="deleteLicenseButton" style="display: none;" onclick="deleteRegister();" >删除注册</button>
					<span id="goToSystem"  style="display: none;" ><button onclick="window.location.reload();" >访问系统</button></span>
				</td>
			</tr>
		</table>
	</div>
</body>
</html>